<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery"  size="mini">
      <el-form-item label="名称" prop="name" >
        <el-input v-model="queryData.name" placeholder="请输入管理员姓名" clearable class="filter-item"/>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryData.phone" placeholder="请输入手机号" clearable maxlength="11" minlength="11" class="filter-item"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
        <el-button style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="handleCreate">添加</el-button>
        <el-button style="margin-left: 10px;" type="primary" icon="el-icon-refresh-left" @click="clear">清空</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="listLoading" :data="list" border highlight-current-row >
      <el-table-column label="ID" prop="id" align="center" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" prop="name" align="center" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号"  align="center">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="说明" align="center">
        <template slot-scope="{row}">
          <span>{{ row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色" align="center">
        <template slot-scope="{row}">
          <span>{{ row.role_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">编辑</el-button>
          <el-button type="danger" size="mini"  @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.num" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="postForm" :rules="rules" :model="postForm" label-width="120px">
        <el-form-item label="手机号" prop="phone" >
          <el-input v-model="postForm.phone" placeholder="请输入手机号" maxlength="11" style="width: 300px;" minlength="11" class="filter-item"/>
        </el-form-item>
        <el-form-item label="姓名" prop="real_name" >
          <el-input v-model="postForm.name" placeholder="请输入管理员姓名"  style="width: 300px;" class="filter-item"/>
        </el-form-item>
        <el-form-item label="管理角色" prop="role_id">
          <el-select v-model="postForm.role_id" placeholder="请选择角色" style="width: 300px;">
            <el-option v-for="item in categoryOptions" :key="item.id" :label="item.name" :value="item.id"  class="filter-item"/>
          </el-select>
        </el-form-item>
        <el-form-item label="说明" prop="remark" >
          <el-input v-model="postForm.remark" placeholder="请输入说明"  style="width: 300px;" class="filter-item"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm('postForm')">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import  {addStaff,getStaff,delStaff,getRole} from '@/api/staff'

export default {
  name: 'ComplexTable',
  components: { Pagination },
  data() {
    //手机号码校验
    var checkphone = (rule, value, callback) => {
      // let phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else if (!this.isCellPhone(value)) { // 引入methods中封装的检查手机格式的方法
        callback(new Error('请输入正确的手机号!'))
      } else {
        callback()
      }
    }
    return {
      categoryOptions: [
        { id: '1', name: '项目组一' },
        { id: '2', name: '项目组二' },
        { id: '3', name: '项目组三' },
        { id: '4', name: '项目组四' }
      ],
      list: [],
      total: 4,
      layout: 'total, sizes, prev, pager, next, jumper',
      listLoading: true,
      listQuery: {
        page: 1,
        sort: '+id',
        num:10
      },
      postForm: {},
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加'
      },
      queryData:{},
      rules: {
        // phone: [{ required: true, message: '请输入正确手机号', trigger: 'blur' }],
        name: [{  required: true, message: '请输入姓名', trigger: 'blur' }],
        role_id: [{ required: true, message: '请选择角色', trigger: 'change' }],
        phone: [{required: true, validator: checkphone, trigger: 'blur'}]
      },
    }
  },
  created() {
    this.getList()
    this.getRoleData()
  },
  watch:{
    dialogFormVisible(val){
      if(!val){
        this.postForm = {}
      }
    }
  },
  methods: {
    async getList() {
      let params = {
        page:this.listQuery.page,
        pageSize:this.listQuery.num
      }
      Object.assign(params,this.queryData)
      this.listLoading = true
      let data = await getStaff(params)
      if(data.code == 200){
        let resData = data.data
        this.list = resData.list
        this.total = resData.total
        this.listLoading = false
      }else{
        this.$message.error(data.userMsg)
      }
    },
    //获取角色
    async getRoleData(){
     let data = await getRole()
      if(data.code == 200){
        let resData = data.data
        this.categoryOptions = resData.list
      }else{
        this.$message.error(data.userMsg)
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.createStaff()
        } else {
          return false;
        }
      });
    },
    async createStaff(){
      let data = await addStaff(this.postForm)
      if(data.code == 200){
        this.$message.success('添加成功')
        this.dialogFormVisible = false
        this.getList()
      }else{
        this.$message.error(data.userMsg)
      }
    },
    //手机号码校验
    isCellPhone (val) {
      if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(val)) {
        return false
      } else {
        return true
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    },
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.postForm = row
    },
    handleDelete(row) {
      this.$confirm('确定禁封吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.delete(row)
      }).catch(() => {})
    },
    async delete(row){
      let data = await delStaff(row)
      if(data.code == 200){
        this.$message.success('已禁封')
        this.getList()
      }else{
        this.$message.error(data.userMsg)
      }
    },
    clear(){
      this.queryData = {}
    }
  }
}
</script>
